<template>
  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="商品名称">
      <el-input v-model="form.name"  placeholder="商品名称"></el-input>
    </el-form-item>

    <el-form-item label="商品类型">
      <el-cascader
        v-model="value"
        :options="options"
        @change="handleChange"
        style="width:500px"
         placeholder="请选择商品的类型"
        
      ></el-cascader>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" plain>主要按钮</el-button>
    </el-form-item>

<el-form-item label="商品图片">
  <el-select v-model="value1" multiple placeholder="请从图库列表中选择商品需要的图片id" style="width:500px" @change="Change">
    <el-option
      v-for="item in fileList"
      :key="item.id"
      :label="item.id"
      :value="item.id"
      >
    </el-option>
  </el-select>
</el-form-item>
 

    <el-form-item label="图库列表" >

      

      
       <ul class="infinite-list"  style="overflow:auto"  >
        <el-upload
          class="upload-demo"
          :action="uploadActionUrl"
          :on-preview="handlePreview"
          
          :file-list="fileList"
          list-type="picture"
        ></el-upload>
       </ul>
      
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">立即创建</el-button>
      <el-button native-type="reset" >重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { GetImage } from "../api/files";
export default {
  data() {
    return {
      form: {
        name: "",
      },
      input: "",
      value: [],
      //图片数组
      fileList: [],
      drawer: false,
      options: [
        {
          value: "女装",
          label: "女装",
          children: [
            {
              value: "1",
              label: "上装",
              children: [
                {
                  value: "Txue",
                  label: "T恤",
                },
                {
                  value: "chenshan",
                  label: "衬衫",
                },
                {
                  value: "maoyi",
                  label: "毛衣",
                },
                {
                  value: "majia",
                  label: "马甲",
                },
              ],
            },
            {
              value: "2",
              label: "流行趋势",
              children: [
                {
                  value: "xiaoheiqun",
                  label: "小黑裙",
                },
                {
                  value: "babiku",
                  label: "芭比裤",
                },
                {
                  value: "maoyiwaitao",
                  label: "毛衣外套",
                },
                {
                  value: "kuotuishenku",
                  label: "阔腿神裤",
                },
                {
                  value: "xizhuangdayi",
                  label: "西装大衣",
                },
                {
                  value: "baozangyangmao",
                  label: "宝藏羊毛",
                },
              ],
            },
          ],
        },
        {
          value: "2",
          label: "男装",
          children: [
            {
              value: "3",
              label: "上装",
              children: [
                {
                  value: "Txue",
                  label: "T恤",
                },
                {
                  value: "chenshan",
                  label: "衬衫",
                },
                {
                  value: "weiyi",
                  label: "卫衣",
                },
                {
                  value: "daunwaitao",
                  label: "短外套",
                },
                {
                  value: "yurongfu",
                  label: "羽绒服",
                },
                {
                  value: "fengyi",
                  label: "风衣",
                },
                {
                  value: "piyi",
                  label: "皮衣",
                },
              ],
            },
            {
              value: "4",
              label: "裤子",
              children: [
                {
                  value: "gongzhuangku",
                  label: "工装裤",
                },
                {
                  value: "xiuxianku",
                  label: "休闲裤",
                },
                {
                  value: "gognzhuagnduanku",
                  label: "工装短裤",
                },
                {
                  value: "xiuxianduanku",
                  label: "休闲短裤",
                },
                {
                  value: "niuzaiku",
                  label: "牛仔裤",
                },
                {
                  value: "niuzaiduanku",
                  label: "牛仔短裤",
                },
              ],
            },
          ],
        },
        {
          value: "3",
          label: "电子产品",
          children: [
            {
              value: "Vivoshouji",
              label: "Vivo手机",
              children: [
                {
                  value: "IQOONeo1",
                  label: "VivoIQOONeo1",
                },
                {
                  value: "IQOONeo2",
                  label: "VivoIQOONeo2",
                },
                {
                  value: "IQOONeo3",
                  label: "VivoIQOONeo3",
                },
                {
                  value: "IQOONeo4",
                  label: "VivoIQOONeo4",
                },
                {
                  value: "IQOONeo5",
                  label: "VivoIQOONeo5",
                },
              ],
            },
            {
              value: "shoujipeijian",
              label: "手机配件",
              children: [
                {
                  value: "shoujike",
                  label: "手机壳",
                },
                {
                  value: "shoujitiemo",
                  label: "手机贴膜",
                },
                {
                  value: "shoujierji",
                  label: "手机耳机",
                },
                {
                  value: "shujuxian",
                  label: "数据线",
                },
                {
                  value: "chongdianbao",
                  label: "充电宝",
                },
                {
                  value: "zipaigan",
                  label: "自拍杆",
                },
                {
                  value: "shoujizhijia",
                  label: "手机支架",
                },
                {
                  value: "fangchensai",
                  label: "防尘塞",
                },
                {
                  value: "shoujiguajian",
                  label: "手机挂件",
                },
                {
                  value: "shoujichongdianqi",
                  label: "手机充电器",
                },
                {
                  value: "chezaizhijia",
                  label: "车载支架",
                },
              ],
            },
            {
              value: "diannaobijiben",
              label: "电脑笔记本",
              children: [
                {
                  value: "shubiaojianpan",
                  label: "鼠标键盘",
                },
                {
                  value: "xianshiqi",
                  label: "显示器",
                },
                {
                  value: "pingbandiannao",
                  label: "平板电脑",
                },
                {
                  value: "zhengtiyitiji",
                  label: "整体一体机",
                },
                {
                  value: "Lenovo",
                  label: "联想笔记本",
                  children: [
                    {
                      value: "Lenovoxiaoxinchao",
                      label: "联想小新潮",
                    },
                    {
                      value: "Lenovoxiaozhengjiuzhe",
                      label: "联想拯救者",
                    },
                  ],
                },
              ],
            },
          ],
        },

        {
          value: "4",
          label: "零食",
          children: [
            {
              value: "ertonglingshi",
              label: "儿童零食",
            },
            {
              value: "wanghongIp",
              label: "网红Ip",
            },
            {
              value: "lingshidalibao",
              label: "零食大礼包",
            },
            {
              value: "quweishiwan",
              label: "趣味食玩",
            },
          ],
        },
      ],
      value1:[]
       
    };
  },
  methods: {
    onSubmit() {
      console.log("submit!");
      console.log(this.value);
      console.log(this.value1);
    },
    handleChange(label) {
      console.log(label);
    },
    Change(){
      console.log(this.value1);
    },
    handlePreview(file) {
      console.log(file);
      console.log(file.indexOf);
      // this.fileList.splice(file.indexOf,1)
      
    },
  
    fetchData(){
      GetImage()
      .then((data) => {
        var res = data.data;
        console.log(res);
        for (var index = 0; index < res.length; index++) {
          var obj = {
            indexOf:index,
            id: res[index].id,
            name: res[index].id ,
            url: "http://localhost:5000/files/" + res[index].id,
          };
          
          this.fileList.push(obj);
        }
      })
      .catch((err) => {
        console.log(err);
      });
    }
  },
  mounted() {
    this.fetchData()
    console.log(this.fileList);
  },
  computed: {
    uploadActionUrl() {
      const baseUrl = "http://localhost:5000/";
      const uploadUrl = "files/uploadfiles";
      return baseUrl + uploadUrl;
    },
  },
};
</script>
<style scoped>
.upload-demo{
  max-height: 300px;
}
</style>